import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _isEmpty from "lodash/isEmpty";
import uuidv1 from 'uuid/v1';
import { getResponse, getCurrentOrganizationId, createPagination } from 'utils/utils';
import { queryMembers as _queryMembers, queryRolesLevelUserCount as _queryRolesLevelUserCount, queryRoles as _queryRoles, // queryUser,
queryCreateRolesSublist as _queryCreateRolesSublist, queryCode as _queryCode, deleteMember as _deleteMember, deleteOrganizationMember, batchAssign, queryHrunitsTree as _queryHrunitsTree } from "../services/memberRoleService";
var tableState = {
  list: [],
  pagination: {
    pageSize: 10,
    total: 0,
    current: 1
  }
};
export default {
  namespace: 'memberRole',
  state: {
    code: {},
    currentAdminRole: {},
    members: _objectSpread({}, tableState),
    roles: {
      list: []
    },
    createRolesSublist: [],
    hrunitsTree: []
  },
  effects: {
    queryMembers: /*#__PURE__*/_regeneratorRuntime.mark(function queryMembers(_ref, _ref2) {
      var payload, put, call, organizationId, response, content, pagination;
      return _regeneratorRuntime.wrap(function queryMembers$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              organizationId = getCurrentOrganizationId();
              _context.next = 5;
              return call(_queryMembers, payload, organizationId);

            case 5:
              response = _context.sent;

              if (!(response && !response.failed)) {
                _context.next = 11;
                break;
              }

              content = response.content;
              pagination = createPagination(response);
              _context.next = 11;
              return put({
                type: 'updateStateReducer',
                payload: {
                  members: {
                    list: content || [],
                    pagination: pagination
                  }
                }
              });

            case 11:
            case "end":
              return _context.stop();
          }
        }
      }, queryMembers);
    }),
    queryRolesLevelUserCount: /*#__PURE__*/_regeneratorRuntime.mark(function queryRolesLevelUserCount(_ref3, _ref4) {
      var payload, call, put, organizationId, response;
      return _regeneratorRuntime.wrap(function queryRolesLevelUserCount$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              organizationId = getCurrentOrganizationId();
              _context2.next = 5;
              return call(_queryRolesLevelUserCount, payload, organizationId);

            case 5:
              response = _context2.sent;
              _context2.next = 8;
              return put({
                type: 'updateStateReducer',
                payload: {
                  roles: {
                    list: getResponse(response) || []
                  }
                }
              });

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, queryRolesLevelUserCount);
    }),
    setRoleMember: /*#__PURE__*/_regeneratorRuntime.mark(function setRoleMember(_ref5, _ref6) {
      var payload, call, put, organizationId, res, response, _response$content, content;

      return _regeneratorRuntime.wrap(function setRoleMember$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              call = _ref6.call, put = _ref6.put;
              organizationId = getCurrentOrganizationId();
              _context3.next = 5;
              return call(_queryRoles, {
                roleId: payload
              }, organizationId);

            case 5:
              res = _context3.sent;
              response = getResponse(res) || {};
              _response$content = response.content, content = _response$content === void 0 ? [] : _response$content;
              _context3.next = 10;
              return put({
                type: 'updateMemberNodeChildrenReducer',
                payload: {
                  id: payload,
                  children: content.map(function (n) {
                    return _objectSpread(_objectSpread({}, n), {}, {
                      name: n.name || n.loginName,
                      key: "member-".concat(uuidv1()),
                      roleId: payload
                    });
                  })
                }
              });

            case 10:
            case "end":
              return _context3.stop();
          }
        }
      }, setRoleMember);
    }),
    queryMember: /*#__PURE__*/_regeneratorRuntime.mark(function queryMember(_ref7, _ref8) {
      var payload, call, res, response, _response$content2, content;

      return _regeneratorRuntime.wrap(function queryMember$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref7.payload;
              call = _ref8.call;
              _context4.next = 4;
              return call(_queryMembers, payload);

            case 4:
              res = _context4.sent;
              response = getResponse(res) || {};
              _response$content2 = response.content, content = _response$content2 === void 0 ? [] : _response$content2;
              return _context4.abrupt("return", content[0]);

            case 8:
            case "end":
              return _context4.stop();
          }
        }
      }, queryMember);
    }),
    queryRoles: /*#__PURE__*/_regeneratorRuntime.mark(function queryRoles(_ref9, _ref10) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function queryRoles$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref9.payload;
              call = _ref10.call;
              _context5.next = 4;
              return call(_queryRoles, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, queryRoles);
    }),
    // *queryUser({ payload }, { call }) {
    //   const organizationId = getCurrentOrganizationId();
    //   const res = yield call(queryUser, payload, organizationId);
    //   return res;
    // },
    queryCreateRolesSublist: /*#__PURE__*/_regeneratorRuntime.mark(function queryCreateRolesSublist(_ref11, _ref12) {
      var payload, call, put, organizationId, res, response;
      return _regeneratorRuntime.wrap(function queryCreateRolesSublist$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref11.payload;
              call = _ref12.call, put = _ref12.put;
              organizationId = getCurrentOrganizationId();
              _context6.next = 5;
              return call(_queryCreateRolesSublist, payload, organizationId);

            case 5:
              res = _context6.sent;
              response = getResponse(res);
              _context6.next = 9;
              return put({
                type: 'updateStateReducer',
                payload: {
                  createRolesSublist: response || []
                }
              });

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, queryCreateRolesSublist);
    }),
    queryCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryCode(_ref13, _ref14) {
      var payload, put, call, response;
      return _regeneratorRuntime.wrap(function queryCode$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref13.payload;
              put = _ref14.put, call = _ref14.call;
              _context7.next = 4;
              return call(_queryCode, payload);

            case 4:
              response = _context7.sent;

              if (!(response && !response.failed)) {
                _context7.next = 9;
                break;
              }

              _context7.next = 8;
              return put({
                type: 'setCodeReducer',
                payload: _defineProperty({}, payload.lovCode, response || [])
              });

            case 8:
              return _context7.abrupt("return", response);

            case 9:
            case "end":
              return _context7.stop();
          }
        }
      }, queryCode);
    }),
    createMember: /*#__PURE__*/_regeneratorRuntime.mark(function createMember(_ref15, _ref16) {
      var memberIds, params, isEdit, call, organizationId, res;
      return _regeneratorRuntime.wrap(function createMember$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              memberIds = _ref15.memberIds, params = _ref15.params, isEdit = _ref15.isEdit;
              call = _ref16.call;
              organizationId = getCurrentOrganizationId();
              _context8.next = 5;
              return call(batchAssign, memberIds, params, isEdit, organizationId);

            case 5:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 7:
            case "end":
              return _context8.stop();
          }
        }
      }, createMember);
    }),
    deleteMember: /*#__PURE__*/_regeneratorRuntime.mark(function deleteMember(_ref17, _ref18) {
      var params, call, organizationId, res;
      return _regeneratorRuntime.wrap(function deleteMember$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              params = _ref17.params;
              call = _ref18.call;
              organizationId = getCurrentOrganizationId();

              if (!(organizationId !== 0)) {
                _context9.next = 9;
                break;
              }

              _context9.next = 6;
              return call(deleteOrganizationMember, params, organizationId);

            case 6:
              res = _context9.sent;
              _context9.next = 12;
              break;

            case 9:
              _context9.next = 11;
              return call(_deleteMember, params);

            case 11:
              res = _context9.sent;

            case 12:
              return _context9.abrupt("return", res);

            case 13:
            case "end":
              return _context9.stop();
          }
        }
      }, deleteMember);
    }),
    queryHrunitsTree: /*#__PURE__*/_regeneratorRuntime.mark(function queryHrunitsTree(_ref19, _ref20) {
      var organizationId, payload, call, put, res, response;
      return _regeneratorRuntime.wrap(function queryHrunitsTree$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              organizationId = _ref19.organizationId, payload = _ref19.payload;
              call = _ref20.call, put = _ref20.put;
              _context10.next = 4;
              return call(_queryHrunitsTree, payload, organizationId);

            case 4:
              res = _context10.sent;
              response = getResponse(res);
              _context10.next = 8;
              return put({
                type: 'updateStateReducer',
                payload: {
                  hrunitsTree: !_isEmpty(response) ? response : []
                }
              });

            case 8:
            case "end":
              return _context10.stop();
          }
        }
      }, queryHrunitsTree);
    })
  },
  reducers: {
    updateStateReducer: function updateStateReducer(state, _ref21) {
      var payload = _ref21.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updateMemberNodeChildrenReducer: function updateMemberNodeChildrenReducer(state, _ref22) {
      var payload = _ref22.payload;
      var id = payload.id,
          children = payload.children;
      return _objectSpread(_objectSpread({}, state), {}, {
        roles: {
          list: state.roles.list.map(function (n) {
            var m = n;

            if (m.id === id) {
              m.children = children;
            }

            return m;
          })
        }
      });
    },
    setCodeReducer: function setCodeReducer(state, _ref23) {
      var payload = _ref23.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        code: Object.assign(state.code, payload)
      });
    }
  }
};